package inc.gome.dynamic.datasource.controller;


import inc.gome.dynamic.datasource.service.MasterService;
import inc.gome.dynamic.datasource.service.SlaveService;
import inc.gome.dynamic.datasource.service.TranstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DynamicController {


    /*加载到了*/
    @Value("${server-addr}")
    private String nacosURL;

    @Value("${dynamic.params}")
    private List<String> params;

    @Autowired
    private MasterService masterService;

    @Autowired
    private SlaveService slaveService;



    @Autowired
    private TranstationService transtationService;





    @RequestMapping("/")
    @ResponseBody
    public String getValue(){
        System.out.println(params.toString());
        return  nacosURL;
    }



    @RequestMapping("/v1")
    @ResponseBody
    public String v1(Integer id){
        return  masterService.fingById(id).toString();
    }

    @RequestMapping("/v2")
    @ResponseBody
    public String v2(Integer id){
        return  slaveService.findById(id).toString();
    }


    @RequestMapping("/v1_update")
    @ResponseBody
    public String v1Update(Integer id,String name){

        masterService.updateById(id,name);
        return  "success";
    }


    @RequestMapping("/v2_update")
    @ResponseBody
    public String v2Update(Integer id,String name){
        slaveService.updateById(id,name);
        return  "success";
    }




    /*测试多数据源事务控制*/
    @RequestMapping("/tran")
    @ResponseBody
    public String testTransation(String name){
        transtationService.testTranstation(name);
        return  "success";
    }
}
